package com.hbzhit.modules.sys.vo;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hbzhit.common.entity.PageQueryEntity;
import com.hbzhit.modules.sys.entity.SysDictValEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@ApiModel("查询参数")
public class SysDictValQueryVO extends PageQueryEntity {

    @ApiModelProperty(value = "字典类型ID", required = false, example = "1")
    private Long dictId;

    @ApiModelProperty(value = "字典类型Code", required = false, example = "USER_TYPE")
    private String dictCode;

    @ApiModelProperty(value = "编码或名称关键字", required = false, example = "abc")
    private String key;

    /**
     * 查询条件
     */
    public LambdaQueryWrapper<SysDictValEntity> queryWrapper() {
        return new LambdaQueryWrapper<SysDictValEntity>()
                .eq(dictId != null, SysDictValEntity::getDictId, dictId)
                .inSql(StrUtil.isNotEmpty(dictCode), SysDictValEntity::getDictId, StrUtil.format("select id from sys_dict where is_del = 0 and dict_code = '{}'", dictCode))
                .and(StrUtil.isNotEmpty(key), wapper -> wapper
                        .like(SysDictValEntity::getValCode, key)
                        .or()
                        .like(SysDictValEntity::getValName, key)
                )
                .orderByAsc(SysDictValEntity::getValCode);
    }
}
